//
//  KnobControl.m
//  SKPAD
//
//  Created by admin@amonline.com.cn on 15/5/8.
//  Copyright (c) 2015年 hr. All rights reserved.
//

#import "KnobControl.h"

@implementation KnobControl

@dynamic startAngle;
@dynamic endAngle;

//代码创建初始化
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        _minimumValue = 0.0;
        _maximumValue = 1.0;
        _value = 0.0;
        _continuous = YES;

        self.backgroundColor = [UIColor clearColor];
        [self createKnobUI];

    }
    return  self;
}

-(void) createKnobUI
{
    //定义旋钮图层的大小变量
    CGRect knobFrame;
    knobFrame.origin.x = self.frame.origin.x;
    knobFrame.origin.y = self.frame.origin.y;
    knobFrame.size.width = self.frame.size.width/5 * 3;
    knobFrame.size.height = self.frame.size.width/5 * 3;
    
    //由于旋钮图层和view大小不一致，所以中心和图层大小分开定义。初始化旋钮图层大小
    _knob = [[Knob alloc] initWithFrame:knobFrame];
    //定义旋钮图层中心位置,旋钮中心就是控件中心frame
    [_knob updateWithPosition:self.frame];
    
    //初始背景图层大小
    _knobBackground = [[KnobBackground alloc] initWithFrame:self.frame];
    //定义背景图层中心位置,背景中心就是控件中心frame
    [_knobBackground updateWithPosition:self.frame];
    
    //初始化手势
    _gestureRecognizer = [[KnobRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    
    //初始化旋钮参数
    //指针范围,范围是-3.926991~~0.785398,-3.926991-0的角度范围是-225°~~~0°，0-0.785393~0°~~45°；
    _knob.startAngle = -M_PI * 1.25;
    _knob.endAngle = M_PI * 0.25;
    
    //指针位置初始化值
    _knob.knobPointerAngle = _knob.startAngle;
    
    //初始化刻度图层
    _knobGraduation = [[KnobGraduation alloc] init];
    _knobGraduation.startAngle = _knob.startAngle;
    _knobGraduation.endAngle = _knob.endAngle;
    _knobGraduation.realityAngle = _knob.startAngle;
    [_knobGraduation updateWithBounds:self.frame];
    
    
    [self addGestureRecognizer:_gestureRecognizer];
    [self.layer insertSublayer:_knobBackground.trackLayerBackground atIndex:0];
    [self.layer insertSublayer:_knobGraduation.trackLayerGraduation atIndex:1];
    [self.layer insertSublayer:_knobGraduation.trackLayerReality atIndex:2];
    [self.layer insertSublayer:_knob.trackLayerKnob atIndex:3];

}

//手势处理函数
-(void) handleGesture:(KnobRotationGestureRecognizer *)gesture
{
    //计算起点到终点的值 1.5xxx 转换成角度相当于90° 公式X = (角度* M_PI)/180;
    CGFloat midPointAngle = (2 * M_PI + self.startAngle - self.endAngle) / 2
    + self.endAngle;
    
    // 2. Ensure the angle is within a suitable range
    CGFloat boundedAngle = gesture.touchAngle;
    if(boundedAngle > midPointAngle) {
        boundedAngle -= 2 * M_PI;
    } else if (boundedAngle < (midPointAngle - 2 * M_PI)) {
        boundedAngle += 2 * M_PI;
    }
    // 3. Bound the angle to within the suitable range
    boundedAngle = MIN(self.endAngle, MAX(self.startAngle, boundedAngle));
    
    // 4. Convert the angle to a value
    CGFloat angleRange = self.endAngle - self.startAngle;
    CGFloat valueRange = self.maximumValue - self.minimumValue;
    CGFloat valueForAngle = (boundedAngle - self.startAngle) / angleRange
    * valueRange + self.minimumValue;
    
    //NSLog(@"valueForAngle= %f, valueRange = %f, angleRange = %f", valueForAngle, valueRange, angleRange);
    // 5. Set the control to this value
    self.value = valueForAngle;
}
//重绘方法
//-(void)drawRect:(CGRect)rect{

   // NSLog(@"??dsad");
    
//}

-(void) setValue:(CGFloat)value
{
    [self setValue:value animated:YES];
}

-(void) setValue:(CGFloat)value animated:(BOOL)animated
{
    if(value != _value)
    {
        _value = MIN(self.maximumValue, MAX(self.minimumValue, value));
        CGFloat angleRange = self.endAngle - self.startAngle;
        CGFloat valueRange = self.maximumValue - self.minimumValue;
        CGFloat angleForValue = (_value - self.minimumValue) / valueRange * angleRange + self.startAngle;
        [_knob setKnobPointerAngle:angleForValue animated:animated];
        [_knobGraduation setRealityAngle:angleForValue];
    }
}

-(void)setValueAngle:(CGFloat)valueAngle
{
    if(valueAngle != _valueAngle)
    {
        _valueAngle = valueAngle;
        [_knobGraduation setRealityAngle:valueAngle];
    }
}
-(CGFloat)startAngle
{
    return _knob.startAngle;
}

-(void) setStartAngle:(CGFloat)startAngle
{
    _knob.startAngle = startAngle;
}

-(CGFloat)endAngle
{
    return _knob.endAngle;
}

-(void) setEndAngle:(CGFloat)endAngle
{
    _knob.endAngle = endAngle;
}


@end